package haois.module.system.rest;

import haois.constant.Reply;
import haois.constant.ReturnState;
import haois.module.BaseController;
import haois.module.rbac.daoman.Manager;
import haois.module.rbac.service.ManagerService;
import haois.module.system.service.TenCloudService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by RuiHaoZ on 2018/4/25 10:45
 */
@RestController
@RequestMapping("/api/web/picture")
public class PictureController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(PictureController.class);
    @Autowired
    private TenCloudService tenCloudService;
    @Autowired
    private ManagerService managerService;

    public Reply updatePicture(MultipartFile mulFile) {
        String nativePath = imgDirFilePath;
        String preFix = "TEST";
        String cosPath = "/webServer/";
        Reply reply = tenCloudService.updatePicture(mulFile, nativePath, preFix, cosPath);
        return reply;
    }

    @RequestMapping(value = "/manager")
    public String updateHead(@RequestParam(value = "file", required = true) MultipartFile mulFile,
                             HttpServletRequest request) {
        Reply reply = this.updatePicture(mulFile);
        String account = handleToken.getWebSubject(request);
        Manager manager = managerService.getByAccount(account);
        if (reply.getCode().equals(ReturnState.CODE_00)) {
            managerService.updatePicture(manager.getId(), reply.getData().toString());
        }
        return reply.toJson();
    }

    @RequestMapping(value = "")
    public String update(@RequestParam(value = "file", required = true) MultipartFile mulFile) {
        Reply reply = this.updatePicture(mulFile);
        Map map = new HashMap();
        map.put("src", reply.getData());
        map.put("title", mulFile.getOriginalFilename());
        reply.setData(map);
        return reply.toJson();
    }
}
